package controllers;

import dto.Language;
import models.*;
import models.test.Contact;
import org.apache.commons.lang.math.NumberUtils;
import play.Logger;
import play.Play;
import play.modules.siena.QueryWrapper;
import play.mvc.Controller;
import play.mvc.With;
import play.test.Fixtures;
import siena.Query;
import siena.core.Many;
import siena.core.SyncList;
import utils.ImageUtil;
import utils.SienaFixutre;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@With(Secure.class)
public class Test extends Controller{
    public static void selfRef() {
        List<Contact> contacts = Contact.all().fetch();
        render(contacts);
    }

    public static void addSelfRef() {
        Long parentId = params.get("parent", Long.class);
        Long childId = params.get("child", Long.class);

        Contact parent = Contact.findById(parentId);
        Contact child = Contact.findById(childId);
        
        parent.relatedsContact.asList().add(child);

        parent.save();

        redirect("Test.selfRef");
    }

    public static void addNew() {
        Contact c = new Contact();
        c.name = "name_" + Math.random();
        c.save();
        redirect("Test.selfRef");
    }

    public static void  changeName() {
        Long contactId = params.get("contactId", Long.class);
        Contact contact = Contact.findById(contactId);

        contact.name = params.get("name");

        contact.save();
        redirect("Test.selfRef");
    }

    public static void initData() {
        System.out.println("Will load data from data.yml");
        Fixtures.loadModels("data.yml");

        List<Topic> fetch = Topic.all().fetch();

        int i = 0;
        for (Topic one:fetch) {
            one.largePhoto = ImageUtil.getOneLargeImage();
            one.relatedPhoto = ImageUtil.getOneRelatedImage();
            i++;
            for (int j=0; j<i*2; j++) {
                one.moreInfos.asList().add((MoreInfo) MoreInfo.all().fetch().get(j));
            }

            for (int j=0; j<i; j++) {
                RelatedTopic relatedTopic = new RelatedTopic();
                relatedTopic.hostTopic = one.id;
                relatedTopic.relatedTopic = fetch.get(j).id;
                relatedTopic.save();
            }
            one.save();
        }

        Long numTopic = Topic.count();
        Long numMoreInfo = MoreInfo.count();

        render(numTopic, numMoreInfo);
    }
    public static void initTopic() {
        Logger.debug("begin=" + Topic.all().fetch().size());

        Logger.debug("Purge all data of Topic");
        Logger.debug("Number of deleted=" + Topic.deleteAll());
        Logger.debug("remain=" + Topic.all().count());

        Logger.debug("Will load data from topic.yml");
        try {
            SienaFixutre.load("topic.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.debug("Number of topic after load topic.yml=" + Topic.all().count());

        addLargeImage();

        redirect("/test/index");
    }

    private static void addLargeImage() {
        Map<String, String> largeImageMap = new HashMap<String, String> ();
        largeImageMap.put("standard_spray_nozzles_en", "standard_nozzles_main.jpg");
        largeImageMap.put("quick-connect_nozzles_en", "quick_connect_main.jpg");
        largeImageMap.put("air-atomizing_nozzles_en", "air-atomizing_main.jpg");
        largeImageMap.put("spray_dry_en", "spraydry_main.jpg");
        largeImageMap.put("compressed_air_en", "compressed_air_main.jpg");
        largeImageMap.put("automatic_nozzles_en", "automatic_main.jpg");
        largeImageMap.put("blower_air_en", "blower_air_main.jpg");

        largeImageMap.put("spray_guns_en", "spray_guns_main.jpg");
        largeImageMap.put("high-pressure_nozzles_en", "high-pressure_main.jpg");
        largeImageMap.put("special_purpose_nozzles_en", "special_purpose_main.jpg");
        largeImageMap.put("accessories_strainers_en", "accessories_strainers_main.jpg");
        largeImageMap.put("accessories_connectors_en", "accessories_connectors_main.jpg");
        largeImageMap.put("products_more_en", "more_spray-nozzles_main.jpg");

        //app market
        largeImageMap.put("chemical_en", "chem_main.jpg");
        largeImageMap.put("food_en", "food_main.jpg");
        largeImageMap.put("paper_en", "paper_main.jpg");
        largeImageMap.put("petrochemical_en", "petrochem_main.jpg");
        largeImageMap.put("pharmaceutical_en", "pharm_main.jpg");
        largeImageMap.put("steel_en", "steel_main.jpg");
        largeImageMap.put("car_wash_en", "car-wash_main.jpg");
        largeImageMap.put("composite_woods_en", "composite-woods_main.jpg");
        largeImageMap.put("markets_more_en", "gas-cool-cond_main.jpg");
        largeImageMap.put("cleaning_washing_en", "clean-wash_main.jpg");
        largeImageMap.put("coating_en", "coating_main.jpg");
        largeImageMap.put("dust_control_en", "dust_main.jpg");
        largeImageMap.put("fire_protection_en", "fire_main.jpg");
        largeImageMap.put("gas_cool_condition_en", "gas-cool-cond_main.jpg");
        largeImageMap.put("humidification_en", "humidification_main.jpg");
        largeImageMap.put("nox_control_en", "NOx_main.jpg");
        largeImageMap.put("spray_nozzles_overview_en", "spray-nozzles_section-header.jpg");
        largeImageMap.put("spray_control_overview_en", "spray-control_section-header.jpg");
        largeImageMap.put("spray_analysis_overview_en", "spray-analysis_section-header.jpg");
        largeImageMap.put("spray_fabrication_overview_en", "spray-fabrication_section-header.jpg");


        List<Topic> topics = Topic.all().fetch();
        for (Topic t : topics) {
            String k = t.code + "_" + t.language;
            if (largeImageMap.containsKey(k)) {
                t.largePhoto = ImageUtil.toByte(largeImageMap.get(k));
                t.save();
            }
        }
    }


    public static void initMenu() {
        Logger.debug("Purge all data of Menu");
        Logger.debug("Number of deleted=" + Menu.all(Menu.class).delete());

        Logger.debug("Will load data from Menu.yml");
        try {
            SienaFixutre.load("data/menu.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.debug("Number of topic after load menu.yml=" + Menu.all(Menu.class).count());

        //System.out.println(Menu.all(Menu.class));
        /*
        List<Menu> fetch = Menu.all(Menu.class).fetch();
        for(Menu m : fetch) {
            System.out.println(m.language + "|" + m.menuName + "|" + m.topicCode);
        }
        */

        redirect("/test/index");
    }
    
    public static void index() {
        render();
    }

    public static void initNews() {
        TinTuc.all(TinTuc.class).delete();

        try {
            SienaFixutre.load("data/news.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }

        redirect("/test/index");
    }

    public static void initTopicRelation() {
        List<Topic> allEn = Topic.all().filter(Language.LANGUAGE_KEY, Language.EN_CODE).fetch();
        for (int i=0; i < allEn.size(); i ++) {
            for (int j=i+1; j < allEn.size() ; j++) {
                RelatedTopic rt = new RelatedTopic();
                rt.relatedTopic = allEn.get(j).id;
                rt.hostTopic = allEn.get(i).id;
                
                rt.save();
            }
        }
        redirect("/test/index");
    }

    public static void initTopicSmallPhoto() {
        List<Topic> allEn = Topic.all().filter(Language.LANGUAGE_KEY, Language.EN_CODE).fetch();
        for (int i=0; i < allEn.size(); i ++) {
            allEn.get(i).relatedPhoto = ImageUtil.getOneRelatedImage();

            allEn.get(i).save();
        }
        redirect("/test/index");
    }


}
